package com.ruoyi.develop.inspectSync;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;

import com.ruoyi.develop.factory.factoryMaterialMain.domain.FactoryMaterialMain;
import com.ruoyi.develop.factory.factoryMaterialMain.service.IFactoryMaterialMainService;
import com.ruoyi.develop.factory.factoryTestStandard.domain.FactoryTestStandard;
import com.ruoyi.develop.factory.factoryTestStandard.service.IFactoryTestStandardService;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * 同步化学分析仪数据
 *
 * @author lmm
 * @date 2023-03-01
 */
@RestController
@RequestMapping("/webapi")
public class SyncInspectController extends BaseController {

    public static final String SECRET_KEY = "p8pUMKrsNBqepqXiWyYScO2G6MYt1TdmYOgypOXYQhzjaD7HRkLdc9nAKNaAMzgR";

    @Autowired
    private IFactoryMaterialMainService factoryMaterialMainService;
    @Autowired
    private IGoodsOrderInService goodsOrderInServiceService;
    @Autowired
    private IFactoryTestStandardService factoryTestStandardService;

    /**
     * 更新检测数据
     */
    @PostMapping("/updateByInspectNumber")
    public AjaxResult updateByInspectNumber(HttpServletRequest request,HttpServletResponse response) {
        System.out.println("根据牌号更新检测数据");

        Map<String, Object> retMap = new HashMap<>();
        // 签名不能为空
        String acceptSign = request.getParameter("sign");
        if (StringUtils.isBlank(acceptSign)) {
            retMap.put("success", false);
            retMap.put("code", "1002");
            return AjaxResult.success("签名不能为空！", retMap);
        }

        // 检验编号不能为空
        String inspectNumber = request.getParameter("inspectNumber");
        if (StringUtils.isBlank(inspectNumber)) {
            retMap.put("success", false);
            retMap.put("code", "1003");
            return AjaxResult.success("检验编号不能为空！", retMap);
        }

        // 检验编号不存在
        FactoryMaterialMain factoryMaterialMain  = factoryMaterialMainService.selectByInspectNumber(inspectNumber);
        if(factoryMaterialMain==null){
            retMap.put("success", false);
            retMap.put("code", "1003");
            return AjaxResult.success("检验编号不存在！", retMap);
        }

        // 验证签名
        String plainText = inspectNumber + SECRET_KEY + DateUtils.formatDate(new Date(), "yyyyMMdd");
        String sign = DigestUtils.md5DigestAsHex(plainText.getBytes());
        System.out.println("sign：：：：：：：：："+sign);
        if(!sign.equalsIgnoreCase(acceptSign)){
            retMap.put("success", false);
            retMap.put("code", "1004");
            return AjaxResult.success("签名验证失败！", retMap);
        }

        String cValue = StringUtils.isBlank(request.getParameter("cValue"))?"0":request.getParameter("cValue");
        String pValue = StringUtils.isBlank(request.getParameter("pValue"))?"0":request.getParameter("pValue");
        String sValue = StringUtils.isBlank(request.getParameter("sValue"))?"0":request.getParameter("sValue");
        String alValue = StringUtils.isBlank(request.getParameter("alValue"))?"0":request.getParameter("alValue");
        String siValue = StringUtils.isBlank(request.getParameter("siValue"))?"0":request.getParameter("siValue");
        String feValue = StringUtils.isBlank(request.getParameter("feValue"))?"0":request.getParameter("feValue");
        String cuValue = StringUtils.isBlank(request.getParameter("cuValue"))?"0":request.getParameter("cuValue");
        String mnValue = StringUtils.isBlank(request.getParameter("mnValue"))?"0":request.getParameter("mnValue");
        String mgValue = StringUtils.isBlank(request.getParameter("mgValue"))?"0":request.getParameter("mgValue");
        String znValue = StringUtils.isBlank(request.getParameter("znValue"))?"0":request.getParameter("znValue");
        String niValue = StringUtils.isBlank(request.getParameter("niValue"))?"0":request.getParameter("niValue");
        String tiValue = StringUtils.isBlank(request.getParameter("tiValue"))?"0":request.getParameter("tiValue");
        String crValue = StringUtils.isBlank(request.getParameter("crValue"))?"0":request.getParameter("crValue");
        String moValue = StringUtils.isBlank(request.getParameter("moValue"))?"0":request.getParameter("moValue");

        factoryMaterialMain.setInspectNumber(inspectNumber);
        factoryMaterialMain.setcValue(new BigDecimal(cValue));
        factoryMaterialMain.setpValue(new BigDecimal(pValue));
        factoryMaterialMain.setsValue(new BigDecimal(sValue));
        factoryMaterialMain.setAlValue(new BigDecimal(alValue));
        factoryMaterialMain.setSiValue(new BigDecimal(siValue));
        factoryMaterialMain.setFeValue(new BigDecimal(feValue));
        factoryMaterialMain.setCuValue(new BigDecimal(cuValue));
        factoryMaterialMain.setMnValue(new BigDecimal(mnValue));
        factoryMaterialMain.setMgValue(new BigDecimal(mgValue));
        factoryMaterialMain.setZnValue(new BigDecimal(znValue));
        factoryMaterialMain.setNiValue(new BigDecimal(niValue));
        factoryMaterialMain.setTiValue(new BigDecimal(tiValue));
        factoryMaterialMain.setCrValue(new BigDecimal(crValue));
        factoryMaterialMain.setMoValue(new BigDecimal(moValue));

        boolean passFlag = factoryMaterialMainService.checkPass(factoryMaterialMain);
        factoryMaterialMain.setTestPass(passFlag ?"0":"1");

        factoryMaterialMainService.update(factoryMaterialMain,"auto");

        retMap.put("success", true);
        retMap.put("code", "1000");
        return AjaxResult.success("更新成功！", retMap);

    }




}
